﻿using Commonality.DAL.MySQL;
using Commonality.Model;
using Commonality.Model.Tray;
using Merge.BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WZS_Auto_Classifying_Merge
{
    public partial class ConfigForm : Form
    {
        public ConfigForm()
        {
            InitializeComponent();
        }

        private void ConfigForm__Load(object sender, EventArgs e)
        {
            Tray.TrayInit();
            MySqlFunction.InitMySQLFunction();
            Parameter.MySQLParameterLoad();
            Parameter.TrayModelElementLoad();
            MySQLParameterDisplay();
            TrayModelElementDisplay();
        }
        private void MySQLParameterDisplay()
        {
            MySQLHost.Text = MySqlFunction.MySql.DataSource;
            MySQLPassword.Text = MySqlFunction.MySql.Password;
            UserId.Text = MySqlFunction.MySql.UserId;
        }

        private void MySQLHost_TextChanged(object sender, EventArgs e)
        {
            string host = (sender as TextBox).Text;
            Parameter.UpdateMySQLHost(host);
            Log4Net.MsgInfo("修改MySQL主机为：" + host);
        }

        private void MySQLPassword_TextChanged(object sender, EventArgs e)
        {
            string password = (sender as TextBox).Text;
            Parameter.UpdateMySQLPassword(password);
            Log4Net.MsgInfo("修改MySQL连接密码：" + password);
        }

        private void UserId_TextChanged(object sender, EventArgs e)
        {
            string userId = (sender as TextBox).Text;
            Parameter.UpdateMySQLUserId(userId);
            Log4Net.MsgInfo("UserID：" + userId);
        }
        private void TrayModelElementDisplay()
        {
            foreach (var item in Tray.X01)
            {
                X01TextBox.Text += (item + "\r\n");
            }
            foreach (var item in Tray.X03)
            {
                X03TextBox.Text += (item + "\r\n");
            }
            foreach (var item in Tray.X05)
            {
                X05TextBox.Text += (item + "\r\n");
            }
            foreach (var item in Tray.X08)
            {
                X08TextBox.Text += (item + "\r\n");
            }
            foreach (var item in Tray.X13)
            {
                X13TextBox.Text += (item + "\r\n");
            }
            foreach (var item in Tray.X15)
            {
                X15TextBox.Text += (item + "\r\n");
            }
        }

        private void UpdateTrayModelElement_TextBox(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13) return;
            switch ((sender as TextBox).Name)
            {
                case "X01TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X01);
                    Parameter.UpdateTrayModelElement(TrayModel.X01, Tray.X01);
                    break;
                case "X03TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X03);
                    Parameter.UpdateTrayModelElement(TrayModel.X03, Tray.X03);
                    break;
                case "X05TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X05);
                    Parameter.UpdateTrayModelElement(TrayModel.X05, Tray.X05);
                    break;
                case "X08TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X08);
                    Parameter.UpdateTrayModelElement(TrayModel.X08, Tray.X08);
                    break;
                case "X13TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X13);
                    Parameter.UpdateTrayModelElement(TrayModel.X13, Tray.X13);
                    break;
                case "X15TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X15);
                    Parameter.UpdateTrayModelElement(TrayModel.X15, Tray.X15);
                    break;
                default:
                    break;
            }
        }
        private void GetModelSetsInTextBox(object sender, ref List<string> model)
        {
            model.Clear();
            string[] data = (sender as TextBox).Lines;
            foreach (var item in data)
            {
                string[] str = Regex.Split(item, "\\s+", RegexOptions.IgnoreCase);
                foreach (var item_ in str)
                {
                    if (item_ != "" && item_ != null)
                    {
                        model.Add(item_);
                    }
                }
            }
            ListDeWeight(model, ref model);
        }
        /// <summary>
        /// 移除重复元素
        /// </summary>
        /// <param name="inp"></param>
        /// <param name="outp"></param>
        private void ListDeWeight(List<string> inp, ref List<string> outp)
        {
            HashSet<string> data = new HashSet<string>(inp);
            outp.Clear();
            foreach (var item in data)
            {
                outp.Add(item);
            }
        }  
    }
}
